package spfRead;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

import javax.imageio.stream.FileImageInputStream;


import jsdai.lang.ASdaiModel;
import jsdai.lang.SdaiException;
import jsdai.lang.SdaiIterator;
import jsdai.lang.SdaiModel;
import jsdai.lang.SdaiRepository;
import jsdai.lang.SdaiSession;
import jsdai.lang.SdaiTransaction;
import jsdai.xml.EarlyBindingV2Reader;
import jsdai.xml.InstanceReader;

public class ReadSpf {

	public static void main(String[] args) throws SdaiException, IOException{

		//create properties
		Properties prop= new Properties();
		//set working directories
		String userdir=System.getProperty("user.dir");
		prop.setProperty("repositories", userdir+"/Examples2");
		//Set properties to SDaiSession
		SdaiSession.setSessionProperties(prop);
		//open session
		SdaiSession session = SdaiSession.openSession();
		//start read/write transaction
		SdaiTransaction transaction = session.startTransactionReadWriteAccess();
		SdaiRepository repository=null;
		Properties pr=new Properties();

		File f= new File(prop.getProperty("repositories")+"/sdairepos.properties");
		
		if (f.exists()){
			pr.load(new FileInputStream(new File(prop.getProperty("repositories")+"/sdairepos.properties")));

			String repoName=pr.getProperty("r1");

			if (repoName==null){
				//import STEP file into a new repository
				repository= session.createRepository("new", null);
			}else{
				repository=session.linkRepository(repoName, null);
			}
		}else{
			repository= session.createRepository("new", null);
		}

		//open repository 
		if (!repository.isActive())
			repository.openRepository();
		session.importClearTextEncoding(args[0],repository);
		//		session.importClearTextEncoding("/home/ubuntu/Bureau/ProjetLong/ProjetN7/Exemple1/instance_express_MFHA_Example1.spf",repository);

		//commit data in transaction
		transaction.commit();
		//get SDAIMOdels in the repositpry
		ASdaiModel models =repository.getModels();
		//iterate throw all repository models
		SdaiIterator modelsIt=models.createIterator();
		while(modelsIt.next()){
			//get current Sdai model
			SdaiModel model =models.getCurrentMember(modelsIt);
			//Start read only acces on Sdai model
			if (model.getMode()==SdaiModel.NO_ACCESS)
				model.startReadOnlyAccess();
//			System.out.println("Model found : "+model.getName()+"\r\n");
//			String shemaname=model.getUnderlyingSchemaString();
//			System.out.println("shemafound : "+shemaname+"\r\n");
			//find the graph schema
			InstanceReader reader = new EarlyBindingV2Reader();
			OutputStream out = new FileOutputStream(new File(args[1]));
			//OutputStream out = new FileOutputStream(new File("/home/ubuntu/Bureau/file2.xml")); 
			model.exportXml(out, reader);
			if (model.getMode()==SdaiModel.READ_ONLY)
				model.endReadOnlyAccess();
		}
		repository.closeRepository();
		transaction.endTransactionAccessCommit();
		session.closeSession();
//		System.out.println("\r\n Finish");

	}

}
